<template>
  <div>
    <el-row>
      <search v-model="form" @submit="getTableList">
        <template slot="item">
          <el-form-item label="角色名">
            <el-input v-model="table.query.roleName" placeholder="角色名"></el-input>
          </el-form-item>
        </template>
        <template slot="btns">
          <el-button type="primary" @click="handleAdd" v-if="$hasButton('add')">添加</el-button>
        </template>
      </search>
      <smallCard :top="10">
        <div slot="content">
          <vxe-table border
                     :data="table.list">
            <vxe-table-column field="roleName" title="角色名称" ></vxe-table-column>
            <vxe-table-column field="roleCode" title="角色代号"></vxe-table-column>
            <vxe-table-column field="roleLevel" title="权限等级"></vxe-table-column>
            <vxe-table-column field="roleDescr" title="角色拥有的权限描述"></vxe-table-column>
            <vxe-table-column field="addUser" title="创建人"></vxe-table-column>
            <vxe-table-column field="status" title="状态">
              <template v-slot="{ row}">
                <el-switch
                  :disabled="!$hasButton('edit')"
                  style="display: block"
                  v-model="row.status"
                  active-color="#13ce66"
                  inactive-color="#ff4949"
                  :active-value="1"
                  :inactive-value="0"
                  @change="chageStatus(row)"
                  active-text="启用"
                  inactive-text="禁用">
                </el-switch>
              </template>
            </vxe-table-column>
            <vxe-table-column field="addTime" title="添加时间"></vxe-table-column>
            <vxe-table-column field="updateTime" title="修改时间"></vxe-table-column>
            <vxe-table-column title="操作" >
              <template v-slot="{ row}">
                <el-button size="mini" @click="handleEdit(row)" v-if="$hasButton('edit')">编辑</el-button>
                <el-button size="mini" @click="handleAuth(row)" v-if="$hasButton('edit')">绑定权限</el-button>
                <el-button size="mini" @click="handleview(row)" v-if="$hasButton('view')">查看</el-button>
                <el-button size="mini" type="danger" @click="handleDelete( row)" v-if="$hasButton('remove')">删除
                </el-button>
              </template>
            </vxe-table-column>
          </vxe-table>
        </div>
      </smallCard>
    </el-row>
    <aForm v-model="form" :type="editType" :show.sync="visible" width="35%" :title="title" v-if="visible" @close="getTableList"></aForm>
    <roleForm v-model="role" :show.sync="roleVisible" v-if="roleVisible"></roleForm>
  </div>
</template>

<script>
  import search from '@/components/my/TopSerach'
  import smallCard from '_c/my/SmallCard'
  import { ROLE } from '@/api'
  import aForm from './form'
  import roleForm from './roleMenu'

  export default {
    components: {
      search,
      smallCard,
      aForm,
      roleForm
    },
    data() {
      return {
        visible: false,
        roleVisible: false,
        editType: 0,
        title: '详情',
        role: {},
        form: {},
        table: {
          query: {},
          list: []
        }
      }
    },
    computed: {},
    mounted() {
      this.getTableList()
    },
    watch: {
      editType(val) {
        switch (val) {
          case 0:
            this.title = '添加'
                break
          case 1:
            this.title = '编辑'
            break
          case 2:
            this.title = '详情'
            break
        }
      }
    },
    methods: {
      getTableList() {
        ROLE.list(this.table.query).then(res => {
          if (res.code === 200) {
            this.table.list = res.data
          } else {
            this.table.list = []
          }
        })
      },
      handleAuth(row) {
        this.role = row
        this.roleVisible = true
      },
      handleAdd() {
        this.form = {
          status: 1
        }
        this.editType = 0
        this.visible = true

      },
      handleEdit(row) {
        this.form = row
        this.visible = true
        this.editType = 1
      },
      handleview(row) {
        this.form = row
        this.visible = true
        this.editType = 2
      },
      handleDelete(row) {
        this.$confirm(`确定要删除${row.roleName}?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          ROLE.remove([row.uuid]).then(res => {
            if (res.code === 200 ) {
              this.$message.success(res.message)
            } else {
              this.$message.error(res.message)
            }
            this.getTableList()
          })
        }).catch(() => {
        })
      },
      chageStatus(val) {
        ROLE.update(val).then(res => {
          if (res.code === 200) {
            this.$message.success(res.message)
          } else {
            this.$message.error(res.message)
          }
          // this.getTableList()
        })

      }
    }
  }
</script>
<style lang="scss" scoped>
</style>
